package com.decard.baselibrarydemo;


import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.decard.baselibrarydemo.model.IdCardBean;
import com.decard.baselibrarydemo.model.QRCodeCallback;
import com.decard.baselibrarydemo.model.ReadCardUtilsCallback;
import com.decard.baselibrarydemo.model.SocialSecurityCardBean;
import com.decard.baselibrarydemo.utils.ReadCardUtilsPad;


@SuppressLint("MissingPermission")
public class MainActivity extends FragmentActivity  implements View.OnClickListener {


    private TextView tv_id;
    private TextView tv_social;
    private TextView tv_qr_code;
    private TextView tv_id_stop;
    private ImageView iv_id_head;


    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLifecycle().addObserver(ReadCardUtilsPad.getInstance(this));
        initView();
    }



    private void initView() {


        tv_id_stop = findViewById(R.id.tv_id_stop);
        iv_id_head = findViewById(R.id.iv_id_head);
        tv_id = findViewById(R.id.tv_id);
        tv_id.setOnClickListener(this);
        tv_social = findViewById(R.id.tv_social);
        tv_social.setOnClickListener(this);
        tv_qr_code = findViewById(R.id.tv_qr_code);
        tv_qr_code.setOnClickListener(this);
        tv_id_stop = findViewById(R.id.tv_id_stop);
        tv_id_stop.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_id:{
                ReadCardUtilsPad.getInstance(this).readIdCard(new ReadCardUtilsCallback() {

                    @Override
                    public void idCardData(IdCardBean idCardBean) {
                        Log.d("__idCardData",idCardBean.toString());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                iv_id_head.setImageBitmap(idCardBean.getPhotoBitmap());

                            }
                        });
                    }

                    @Override
                    public void socialSecurityCardData(SocialSecurityCardBean socialSecurityCardBean) {

                    }

                    @Override
                    public void nullCard() {

                    }

                    @Override
                    public void errMsg(String errMsg) {

                    }
                });
                break;
            }

            case R.id.tv_id_stop:{
                ReadCardUtilsPad.getInstance(this).readIdCardStop();
                break;
            }

            case R.id.tv_social:{
                ReadCardUtilsPad.getInstance(this).readSocial(new ReadCardUtilsCallback(){
                    @Override
                    public void idCardData(IdCardBean idCardBean) {

                    }

                    @Override
                    public void socialSecurityCardData(SocialSecurityCardBean socialSecurityCardBean) {

                    }

                    @Override
                    public void nullCard() {

                    }

                    @Override
                    public void errMsg(String errMsg) {

                    }
                });
                break;
            }

            case R.id.tv_qr_code:{
                ReadCardUtilsPad.getInstance(this).scanQRCode(new QRCodeCallback(){

                    @Override
                    public void result(String str) {
                        tv_qr_code.setText(str);
                    }
                });

            }

            default:
                break;
        }
    }
}



